package com.esisa.IHM;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileChooser extends JPanel implements ActionListener  {
	
	private JButton openButton; // button to open the selected file
	private JFileChooser fc; // JFileChooser used to open files
	private String choosedFile; // choosed file field
	private JTextField selectedDirectory; // selected directory text field
	//private LangFileParser lang; // used to support multi language
	
	public FileChooser() {
		// TODO Auto-generated constructor stub
	}
	
	
	public FileChooser(boolean border) {
		
		// Set the JPanel layout manager
		setLayout(new BorderLayout());
		// Create a JPanel which contains buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		// Set the JPanel border if border==true
		if(border)
		buttonPanel.setBorder(BorderFactory.createTitledBorder("Ouvrir"));
		// Create the selected directory text field
		selectedDirectory = new JTextField(System.getProperty("user.home"), 35);
		// Create the file chooser open button
		//openButton = new JButton(lang.getValue(this.getClass().getName(), "openButton"));
		openButton.addActionListener(this);
		// Add elements into the buttonPanel
		buttonPanel.add(selectedDirectory);
		buttonPanel.add(openButton);
		// Add the buttonPanel into the parent JPanel
		add(buttonPanel, BorderLayout.NORTH);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == openButton){
			fc = new JFileChooser(selectedDirectory.getText());
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int returnVal = fc.showOpenDialog(FileChooser.this);
		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			choosedFile = fc.getSelectedFile().getPath();
			selectedDirectory.setText(choosedFile);
		}
		} 
	}
	
	// Retun the selected file
	public String getSelectedFile() {
		
		return choosedFile;
	}
	
	

}
